package com.spring.oaassist.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.spera.shopoa.util.ShopOaSessionUtil;
import com.spera.shopoa.util.ShopOaSessionUtil.OaUser;
import com.spring.oaassist.web.service.OcrmReportService;
import com.spring.security.core.annotation.OaUserPermission;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.controller.BaseController;
import com.spring.security.web.model.BaseResult;

@Controller
@RequestMapping(value = "/report")
public class OcrmReportController extends BaseController{
	
	@Resource OcrmReportService ocrmReportService;
	
	/**
	 * 人员签署合同明细表
	 * @param request
	 * @return
	 */
//	@OaUserPermission(value="/report/htmxList")
	@RequestMapping(value = "/htmxList")
    public ModelAndView htmxList(HttpServletRequest request){
		return new ModelAndView("main/ocrmreport/htmxlist");
    }
	
	@RequestMapping(value = "/queryHtmxList")
    @ResponseBody
    public Object queryHtmxList(String querydate,HttpServletRequest request){
		if(!ApplicationUtils.isNotNull(querydate)){
			return null;
		}
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
//		if(oaUser!=null&&ApplicationUtils.isNotNull(oaUser.getOa_user_id())&&ApplicationUtils.isNotNull(querydate)){
			Map<String, Object> parm = new HashMap<String, Object>();
//			parm.put("kids_parkID",ApplicationUtils.Object2String(oaUser.getKids_park_id()));
			parm.put("year",querydate);
	        List<Map<String,Object>> list = ocrmReportService.queryHtmxList(parm);
	        return list;
//		}else{
//			return null;
//		}
    }
	
	/**
	 * 明细
	 * @param userId
	 * @param nd_yd
	 * @param dateTime
	 * @param request
	 * @return
	 */
//	@OaUserPermission(value="/report/htmxForm")
	@RequestMapping(value = "/htmxDetail")
    public ModelAndView htmxForm(String userId,String nd_yd,String dateTime,HttpServletRequest request){
		Map<String,String> s = new HashMap<String,String>();
		s.put("userId", userId);
		s.put("nd_yd", nd_yd);
		s.put("dateTime", dateTime);
		return new ModelAndView("main/ocrmreport/htmxdetail",s);
    }

	/**
	 * 合同明细明细
	 * @param userId
	 * @param nd_yd
	 * @param dateTime
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryHtmxDetailList")
    @ResponseBody
    public Object queryHtmxDetailList(String userId,String nd_yd,String dateTime,HttpServletRequest request){
		if(!ApplicationUtils.isNotNull(userId)||
				!ApplicationUtils.isNotNull(nd_yd)||
				!ApplicationUtils.isNotNull(dateTime)){
			return null;
		}
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
//		if(oaUser!=null&&ApplicationUtils.isNotNull(oaUser.getOa_user_id())&&ApplicationUtils.isNotNull(querydate)){
			Map<String,Object> parm = new HashMap<String,Object>();
			parm.put("userId", userId);
			parm.put("nd_yd", nd_yd);
			parm.put("dateTime", dateTime);
	        List<Map<String,Object>> list = ocrmReportService.queryHtmxDetailList(parm);
	        return list;
//		}else{
//			return null;
//		}
    }

	/**
	 * 库存资产
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/kczcList")
	public ModelAndView kczcList(HttpServletRequest request){
		return new ModelAndView("main/ocrmreport/kczclist");
	}

	/**
	 * 客户分析
	 * @param request
	 * @return
	 */
	@OaUserPermission(value="/report/khfxList")
	@RequestMapping(value = "/khfxList")
	public ModelAndView khfxList(Model model,HttpServletRequest request){
		OaUser oaUser = ShopOaSessionUtil.getOaUser(request);
		model.addAttribute("deptId",oaUser.getUser_dept_id());
		return new ModelAndView("main/ocrmreport/khfx");
	}

	/**
	 * 客户明细
	 * @param cusType
	 * @param dateTime
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/khfxDetail")
	public ModelAndView khfxDetail(@RequestParam String cusType,@RequestParam String dateTime,HttpServletRequest request){
		Map<String,String> s = Maps.newHashMap();
		s.put("cusType",cusType);
		s.put("dateTime",dateTime);
		return new ModelAndView("main/ocrmreport/khfxdetail",s);
	}

	/**
	 * 柱状图折线图数据
	 * @param dateYear
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/khfxSeries")
	@ResponseBody
	public Object khfxSeries(@RequestParam String dateYear,String deptId,String userId,HttpServletRequest request){
		List<Map<String,Object>> retList = Lists.newArrayList();
		Map<String,String> s = new HashMap<String,String>();
		s.put("dateYear", dateYear);
		s.put("deptId", deptId);
		s.put("userId", userId);
		//普通客户
		Map<String,Object> ptkhRetMap = Maps.newHashMap();
		ptkhRetMap.put("name","普通客户");
		s.put("customerType", "1");
		ptkhRetMap.put("data", ocrmReportService.khfxSeries(s));
		//潜在客户
		Map<String,Object> qzkhRetMap = Maps.newHashMap();
		qzkhRetMap.put("name","潜在客户");
		s.put("customerType", "2");
		qzkhRetMap.put("data", ocrmReportService.khfxSeries(s));
		//VIP客户
		Map<String,Object> vipkhRetMap = Maps.newHashMap();
		vipkhRetMap.put("name","VIP客户");
		s.put("customerType", "3");
		vipkhRetMap.put("data", ocrmReportService.khfxSeries(s));
		//代理商
		Map<String,Object> dlsRetMap = Maps.newHashMap();
		dlsRetMap.put("name","代理商");
		s.put("customerType", "4");
		dlsRetMap.put("data", ocrmReportService.khfxSeries(s));
		//合作伙伴
		Map<String,Object> hzhbRetMap = Maps.newHashMap();
		hzhbRetMap.put("name","合作伙伴");
		s.put("customerType", "5");
		hzhbRetMap.put("data", ocrmReportService.khfxSeries(s));
		//失效客户
		Map<String,Object> sxkhRetMap = Maps.newHashMap();
		sxkhRetMap.put("name","失效客户");
		s.put("customerType", "6");
		sxkhRetMap.put("data", ocrmReportService.khfxSeries(s));
		retList.add(ptkhRetMap);
		retList.add(qzkhRetMap);
		retList.add(vipkhRetMap);
		retList.add(dlsRetMap);
		retList.add(hzhbRetMap);
		retList.add(sxkhRetMap);
		return retList;
	}

	/**
	 * 饼状图数据
	 * @param dateYear
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/khfxPieSeries")
	@ResponseBody
	public Object khfxPieSeries(@RequestParam String dateYear,String deptId,String userId,HttpServletRequest request){
		Map<String,Object> s = new HashMap<String,Object>();
		s.put("dateYear", dateYear);
		s.put("deptId", deptId);
		s.put("userId", userId);
		List<Map<String,Object>>  retList = ocrmReportService.khfxSeriesByYear(s);
		return retList;
	}

	/**
	 * 客户分析列表数据
	 * @param dateYear
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/khfxGridData")
	@ResponseBody
	public Object khfxGridData(@RequestParam String dateYear,String deptId,String userId,
							   HttpServletRequest request){
		Map<String,Object> ret = new HashMap<String,Object>();
		Map<String,Object> s = new HashMap<String,Object>();
		s.put("dateYear", dateYear);
		s.put("deptId", deptId);
		s.put("userId", userId);
		List<Map<String,Object>>  retList = ocrmReportService.khfxGridData(s);
		Integer ptkh=0,qzkh=0,vipkh=0,dlkh=0,hzhb=0,sxkh=0;
		for(Map one:retList){
			ptkh += Integer.parseInt(ApplicationUtils.Object2String(one.get("ptkh")));
			qzkh += Integer.parseInt(ApplicationUtils.Object2String(one.get("qzkh")));
			vipkh += Integer.parseInt(ApplicationUtils.Object2String(one.get("vipkh")));
			dlkh += Integer.parseInt(ApplicationUtils.Object2String(one.get("dlkh")));
			hzhb += Integer.parseInt(ApplicationUtils.Object2String(one.get("hzhb")));
			sxkh += Integer.parseInt(ApplicationUtils.Object2String(one.get("sxkh")));
		}
		Map<String,Object> footer = new HashMap<String,Object>();
		footer.put("serviceTime","合计");
		footer.put("ptkh",ptkh);
		footer.put("qzkh",qzkh);
		footer.put("vipkh",vipkh);
		footer.put("dlkh",dlkh);
		footer.put("hzhb",hzhb);
		footer.put("sxkh",sxkh);
		List<Map<String,Object>>  footerList = Lists.newArrayList();
		footerList.add(footer);
		ret.put("rows",retList);
		ret.put("footer",footerList);
		return ret;
	}

	/**
	 * 客户分析明细列表数据
	 * @param cusType
	 * @param dateTime
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/khfxDetailGridData")
	@ResponseBody
	public Object khfxDetailGridData(@RequestParam String cusType,@RequestParam String dateTime,HttpServletRequest request){
		Map<String,Object> s = new HashMap<String,Object>();
		s.put("cusType", cusType);
		s.put("dateTime", dateTime);
		List<Map<String,Object>>  retList = ocrmReportService.khfxDetailGridData(s);
		return retList;
	}
}
